//
//  TDVerificationCodeViewController.m
//  TFB
//
//  Created by Nothing on 15/3/25.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDVerificationCodeViewController.h"
#import "TDRegistViewController.h"
#import "TDForgetPwdViewController.h"
#import "TDAgreementViewController.h"

@interface TDVerificationCodeViewController ()
{
    NSString *_codeType;
    //
    dispatch_queue_t queue ;
    dispatch_source_t _timer;
}

@property (nonatomic, assign) BOOL isAgreed;

@end

@implementation TDVerificationCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    [self creatUI];
    
}

- (void)setIsAgreed:(BOOL)isAgreed
{
    _isAgreed = isAgreed;
    
    if (_isAgreed) {
        [self.agreeBtn setImage:[UIImage imageNamed:@"checkbox_select"] forState:0];
    } else {
        [self.agreeBtn setImage:[UIImage imageNamed:@"checkbox_normal"] forState:0];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)creatUI
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getVercode)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    self.getVercodeLabel.userInteractionEnabled = YES;
    [self.getVercodeLabel addGestureRecognizer:tap];
    
    
    if (self.isNeedMobileNum) {
        self.phoneNumView.hidden = NO;
    }
}

- (void)getVercode
{
    if (!self.phoneNumView.hidden) {
        if (self.phoneNumTF.text.length < 11) {
            [self.view makeToast:@"请输入正确的手机号" duration:2.0f position:@"center"];
        }
        self.mobileNum = _phoneNumTF.text;
    }
    
    
    if (self.verCodeType == VerRegist) {
        _codeType = @"01";
    }
    else if (self.verCodeType == VerEditLoginPwd){
        _codeType = @"02";
    }
    else if (self.verCodeType == VerEditPayPwd){
        _codeType = @"03";
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [TDHttpEngine requestForGetMsgCodeWithCustMobile:self.mobileNum codeType:_codeType complete:^(BOOL succeed, NSString *msg, NSString *cod) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (succeed) {
            [self timerFireMethod];
            //NSLog(@"%@", msg);
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
        else{
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];

}

//验证码跳秒方法
- (void)timerFireMethod
{
    _getVercodeLabel.userInteractionEnabled = NO;
    __block int timeout = 59; //倒计时时间
    queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                _getVercodeLabel.userInteractionEnabled = YES;
                _getVercodeLabel.text = @"重新获取";
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d秒", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                _getVercodeLabel.text = [NSString stringWithFormat:@"%@",strTime];
                _getVercodeLabel.textColor = [UIColor blackColor];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

- (IBAction)onClickAgreeBtn:(id)sender {
    
    self.isAgreed = !self.isAgreed;
}

- (IBAction)onClickGoAgreeBtn:(id)sender {
    
    TDAgreementViewController *controller = [[TDAgreementViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}


- (IBAction)nextBtnClick:(id)sender {
    if (self.vercodeTF.text.length == 0) {
        [self.view makeToast:@"请输入收到的验证码" duration:2.0f position:@"center"];
        return;
    }
    
    if (self.phoneNumTF.text.length < 11) {
        [self.view makeToast:@"请输入正确的手机号" duration:2.0f position:@"center"];
        return;
    }
    self.mobileNum = _phoneNumTF.text;
    
    if (!self.isAgreed) {
        [self.view makeToast:@"请勾选同意服务协议选项" duration:2.0f position:@"center"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    if (self.verCodeType == VerRegist) {
        _codeType = @"01";
    }
    else if (self.verCodeType == VerEditLoginPwd){
        _codeType = @"02";
    }
    else if (self.verCodeType == VerEditPayPwd){
        _codeType = @"03";
    }
    
    [TDHttpEngine requestForVerifyMsgCodeWithCustMobile:self.mobileNum codeType:_codeType msgCode:self.vercodeTF.text isForgetPwd:self.isForgetPwd complete:^(BOOL succeed, NSString *msg, NSString *cod) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (succeed) {
            
            if (self.verCodeType == VerRegist) {
                TDRegistViewController *registVC = [[TDRegistViewController alloc] init];
                registVC.mobileNum = self.phoneNumTF.text;
                [self.navigationController pushViewController:registVC animated:YES];
            }
            else{
                TDForgetPwdViewController *forgetPwdVC = [[TDForgetPwdViewController alloc] init];
                if (self.verCodeType == VerEditLoginPwd) {
                    forgetPwdVC.isEditLoginPwd = YES;
                    
                }
                if (self.verCodeType == VerEditPayPwd) {
                    forgetPwdVC.isEditLoginPwd = NO;
                }
                forgetPwdVC.isNeedOldPwd = NO;
                forgetPwdVC.value = self.vercodeTF.text;
                forgetPwdVC.custMobile = self.phoneNumTF.text;
                [self.navigationController pushViewController:forgetPwdVC animated:YES];
            }
        }
        else{
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end
